package com.jwell.panrank.presenter

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.jwell.driverapp.tools.BaseSubscriber
import com.jwell.driverapp.ui.base.BasePresenter
import com.jwell.panrank.contract.SearchContract
import com.jwell.panrank.entity.InlineBean
import com.jwell.panrank.entity.SearchBen
import com.jwell.panrank.entity.SearchDriverBean
import com.jwell.panrank.model.SearchModel
import org.json.JSONObject
import org.reactivestreams.Subscription

/**
 * Created by 76577 on 2018/4/10.
 * 各种搜索
 */
class SearchPresenter : SearchContract.Presenter, BasePresenter<SearchContract.View>() {


    private val model: SearchModel by lazy {
        SearchModel()
    }

    //获取货物名称列表
    override fun appGetProList(filter: String?, type: Int) {
        model.appGetProList(filter, type, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                val searchBenList = Gson().fromJson<List<SearchBen>>(data.getString("result"), object : TypeToken<List<SearchBen>>() {
                }.type)
                getView()?.showSearchBenList(searchBenList)
            }

            override fun onFailed(msg: String) {
                getView()?.hideProgress()
                getView()?.toast(msg)
            }
        })
    }

    //获取收发货单位
    override fun appGetUnitInformationList(filter: String?) {
        model.appGetUnitInformationList(filter, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                val searchBenList = Gson().fromJson<List<SearchBen>>(data.getString("result"), object : TypeToken<List<SearchBen>>() {
                }.type)
                getView()?.showSearchBenList(searchBenList)
            }

            override fun onFailed(msg: String) {
                getView()?.hideProgress()
                getView()?.toast(msg)
            }
        })
    }

    //搜索司机
    override fun getDriverList(filter: String?) {
        model.getDriverList(filter, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                val searchBenList = Gson().fromJson<List<SearchDriverBean>>(data.getString("result"), object : TypeToken<List<SearchDriverBean>>() {
                }.type)
                getView()?.showSearchBenList(searchBenList)
            }

            override fun onFailed(msg: String) {
                getView()?.hideProgress()
                getView()?.toast(msg)
            }
        })
    }

    override fun getContractList(filter: String?) {
        model.getContractList(filter, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                val searchBenList = Gson().fromJson<List<InlineBean>>(data.getString("result"), object : TypeToken<List<InlineBean>>() {
                }.type)
                getView()?.showSearchBenList(searchBenList)
            }

            override fun onFailed(msg: String) {
                getView()?.hideProgress()
                getView()?.toast(msg)
            }
        })
    }
}